package com.inossem.warehousestorage_inossem.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.inossem.utils.permission.PermissionUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

public class WelcomeActivity extends BaseActivity {

    private Intent intent;
    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_welcome);
        initView(WelcomeActivity.this);
        initData(WelcomeActivity.this);
        boolean loginFlag = SPUtils.getLoginFlag(WelcomeActivity.this);
//        boolean loginFlag = true;
        intent = new Intent();
        if (loginFlag) {
            intent.setClass(WelcomeActivity.this, MainActivity.class);
        } else {
            intent.setClass(WelcomeActivity.this, LoginActivity.class);
        }

        applyPermission();
    }

    private void initView(Activity activity) {

    }

    private void initData(Activity activity) {

    }

    private void applyPermission () {
        PermissionUtils.applyForPermission(WelcomeActivity.this, PermissionUtils.getManifestPermission(WelcomeActivity.this , PermissionUtils.getAppProcessName(WelcomeActivity.this)), new PermissionUtils.OnApplyPermissionListener() {
            @Override
            public void onSuccessful() {
//                Toast.makeText(WelcomeActivity.this, R.string.successfully, Toast.LENGTH_SHORT).show();
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure() {
                Toast.makeText(WelcomeActivity.this, getResources().getString(R.string.activity_welcome_because_you_rejected_certain_permissions), Toast.LENGTH_SHORT).show();
                startActivity(intent);
                finish();
            }
        });
    }

    @Override
    protected void onActivityResultImpl(int requestCode, int resultCode, Intent data) {
        super.onActivityResultImpl(requestCode, resultCode, data);
        switch (requestCode) {
            case PermissionUtils.REQUEST_CODE_SETTING: {
                if (PermissionUtils.permissionIsAllow(WelcomeActivity.this , PermissionUtils.getManifestPermission(WelcomeActivity.this , PermissionUtils.getAppProcessName(WelcomeActivity.this)))) {
//                    Toast.makeText(WelcomeActivity.this, R.string.successfully, Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    finish();
                }else {
                   applyPermission();
                }
                break;
            }
        }
    }
}
